package com.summer.safe.quartz.controller;

import com.summer.safe.quartz.service.QuartzJobService;
import com.summer.safe.base.BaseController;
import com.summer.safe.base.Page;
import com.summer.safe.config.annotation.ResultWrapper;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.config.result.ResultWrapperEntity;
import com.summer.safe.exception.CustomException;
import com.summer.safe.quartz.entity.QuartzJob;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 定时任务调度Controller
 *
 * @author lm
 * @date 2020-07-23 14:27:23
 */
@Controller
@RequestMapping(value = "/quartz/quartzJob")
public class QuartzJobController extends BaseController {
    private String PREFIX = "/quartz/";

    @Autowired
    private QuartzJobService quartzJobService;

    @ModelAttribute("quartzJob")
    public QuartzJob get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return quartzJobService.get(id);
        }else{
            return new QuartzJob();
        }
    }

    /**
     * 列表页面的跳转
     *
     * @author lm
     * @date 2020-07-23 14:27:23
     */
    @RequiresPermissions("quartz:quartzJob:view")
    @GetMapping("")
    public String index() {
        return PREFIX + "quartzJobList.html";
    }

    /**
     * 获取列表数据
     *
     * @author lm
     * @date 2020-07-23 14:27:23
     */
    @GetMapping("/dataList")
    @ResultWrapper(ResultCode.SUCCESS_QUERY)
    public Page list(QuartzJob quartzJob, HttpServletRequest request) {
        Page<QuartzJob> page = quartzJobService.findPage(new Page(request), quartzJob);
        return page;
    }
    /**
     * 表单页面的跳转
     *
     * @author lm
     * @date 2020-07-23 14:27:23
     */
    @GetMapping("/form")
    @RequiresPermissions("quartz:quartzJob:edit")
    public String form(QuartzJob quartzJob, Model model) {
        model.addAttribute("quartzJob", quartzJob);
        return PREFIX + "quartzJobForm.html";
    }




    /**
     * 提交数据
     *
     *@author lm
     * @date 2020-07-23 14:27:23
     */
    @PostMapping("/save")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public void save(QuartzJob quartzJob) {
        quartzJobService.save(quartzJob);
    }
    /**
     * 提交数据
     *
     *@author lm
     * @date 2020-07-23 14:27:23
     */
    @PostMapping("/updateJobStatus")
    @ResponseBody
    public ResultWrapperEntity updateJobStatus(String id,String status) {
        try {
            quartzJobService.updateJobStatus(new QuartzJob(id,status));
            return ResultWrapperEntity.success("状态更新成功");
        }catch ( Exception e){
            logger.error("抛出的异常{}", e.getMessage());
            return ResultWrapperEntity.error("状态更新失败"+e.getMessage());
        }

    }


    /**
     * 删除数据
     *
     *@author lm
     * @date 2020-07-23 14:27:23
     */
    @PostMapping("/delete")
    @ResultWrapper(ResultCode.SUCCESS_DELETE)
    public void delete(QuartzJob quartzJob) {
        quartzJobService.deleteJob(quartzJob);
    }
}